//
//  HHMessageViewController.m
//  HHBroker
//
//  Created by He Wei on 8/26/15.
//  Copyright © 2015 harry. All rights reserved.
//

#import "HHMessageViewController.h"
#import "HHMessageDetailViewController.h"
#import "HHNewsViewController.h"

@interface HHMessageViewController ()

@end

@implementation HHMessageViewController
@synthesize infoArray, timerArray, myTableView;
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self prepareData];
    titleArray = @[@"添加小计提醒", @"线索即将进入经理库", @"新闻"];

    //titleArray = @[@"添加小计提醒", @"线索即将进入经理库", @"新闻", @"通知", @"公海"];
    iconImageArray = @[@"msg_Addresource", @"msg_Manger", @"msg_news", @"msg_Notification", @"msg_gonghai"];
    urlArray = @[@"addnote", @"inmanager", @"news", @"notify", @"insea"];
    
    infoArray = [[NSMutableArray alloc] initWithArray:@[@"", @"", @"", @"", @""]];
    timerArray = [[NSMutableArray alloc] initWithArray:@[@"", @"", @"", @"", @""]];
    myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kCurrentWidth, kCurrentHeight -49) style:UITableViewStyleGrouped];
    myTableView.delegate = self;
    myTableView.dataSource = self;
    //myTableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 60)];

    myTableView.sectionHeaderHeight = 20;
    [self.view addSubview:myTableView];
    

}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 54;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return titleArray.count;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *strCell = @"myCell";
    HHMessageCell *cell = [tableView dequeueReusableCellWithIdentifier:strCell];
    if (cell == nil) {
        cell = [[HHMessageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:strCell];
    }
    cell.titleLabel.text = titleArray[indexPath.row];
    cell.infoLabel.text = infoArray[indexPath.row];
    cell.imageView.image = [UIImage imageNamed:iconImageArray[indexPath.row]];
    
    NSString *time = timerArray[indexPath.row];
    if (time && time.length > 1) {
        cell.timerLabel.text = [NSString timeStampChinese:time];
    } else {
        cell.timerLabel.text = [NSString timeStampChinese:[NSString currentTimeNumber]];
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 90;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    if (indexPath.row < 2) {
        HHMessageDetailViewController *messageDetail = [[HHMessageDetailViewController alloc] init];
        messageDetail.urlAction = urlArray[indexPath.row];
        messageDetail.nextTitle = titleArray[indexPath.row];
        messageDetail.nextImage = [UIImage imageNamed:iconImageArray[indexPath.row]];
        NSString *time = timerArray[indexPath.row];
        messageDetail.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:messageDetail animated:YES];
    } else if (indexPath.row == 2){
        HHNewsViewController *news = [[HHNewsViewController alloc] init];
        news.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:news animated:YES];
    } else {
    
    
    }

}



- (void)prepareData
{
    __block MBProgressHUD *hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    NSString *urlString = [NSString stringWithFormat:@"%@", MESSAGE_LIST];
    [HttpRequestManager getWithUrlString:urlString success:^(id response) {
        NSInteger code = [[response objectForKey:@"code"] integerValue];
        if (code == 1) {
            NSDictionary *dic = [response objectForKey:@"data"];
            [infoArray replaceObjectAtIndex:0 withObject:[dic objectForKey:@"addnote"]];
            [infoArray replaceObjectAtIndex:1 withObject:[dic objectForKey:@"inmanager"]];
            [infoArray replaceObjectAtIndex:2 withObject:[dic objectForKey:@"news"]];
            [infoArray replaceObjectAtIndex:3 withObject:[dic objectForKey:@"notify"]];
            [infoArray replaceObjectAtIndex:4 withObject:[dic objectForKey:@"insea"]];
            
            [timerArray replaceObjectAtIndex:0 withObject:[dic objectForKey:@"addnote_time"]];
            [timerArray replaceObjectAtIndex:1 withObject:[dic objectForKey:@"inmanager_time"]];
            [timerArray replaceObjectAtIndex:2 withObject:[dic objectForKey:@"news_time"]];
            [timerArray replaceObjectAtIndex:3 withObject:[dic objectForKey:@"notify_time"]];
            [timerArray replaceObjectAtIndex:4 withObject:[dic objectForKey:@"insea_time"]];
            [myTableView reloadData];
            
            [hub hide:YES];

            
            
        } else {
            hub.mode = MBProgressHUDModeText;
            hub.labelText = NSLocalizedString(@"加载失败", @"");
            [hub hide:YES afterDelay:2.f];
        }
    }failure:^(NSError *error) {
        hub.mode = MBProgressHUDModeText;
        hub.labelText = NSLocalizedString(@"网络有点问题!", @"");
        [hub hide:YES afterDelay:2.f];
    }];

}



@end
